/*
ID:luongho3
LANG:JAVA
PROB:ditch
*/
import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;


public class ditch {

	public static int nNodes, nEdges;
	public static final int INF = 2100000100; 
	public static int[][] m = new int[300][300];
	public static boolean[] visited = new boolean[300];
	public static int[] label = new int[300];
	public static int[] prev = new int[300];
	public static int total = 0;
	
	public static void main(String[] args) throws Exception {
		
		Scanner scanner = new Scanner(new File("ditch.in"));
		PrintStream ps = new PrintStream(new File("ditch.out"));
		System.setOut(ps);
		
		nEdges = scanner.nextInt();
		nNodes = scanner.nextInt();
		
		for (int i=1;i<=nEdges;++i) {
			int from = scanner.nextInt();
			int to = scanner.nextInt();
			int capa = scanner.nextInt();
			m[from][to] += capa;
		}
		
		while (true) {
			
			// find the longest path
			for (int i=1;i<=nNodes;++i) {
				visited[i] = false;
				label[i] = 0;
				prev[i] = -1;
			}
			
			label[1] = INF;
			boolean quit = false;
			
			
			
			while (true) {
				
				int idx = -1;
				int max = 0;
				
				for (int i=1;i<=nNodes;++i) {
					if (!visited[i]) {
						if (label[i] > max) {
							max = label[i];
							idx = i;
						}
					}
				}
				if (idx == -1) {
					// not such path exists, break outer loop
					quit = true;
					break;
				}
				else if (idx == nNodes) {
					// reach the sink node
					// update total flow, then break inner loop
					
					int minflow = label[nNodes];
					int current = idx;
					int previous;					
					total += minflow;
					
					current = idx;
					do {
						previous = prev[current];
						m[previous][current] -= minflow;
						m[current][previous] += minflow;
						current = previous;
					} while (previous!=1);
					
					break;
				}
				else {
					visited[idx] = true;
					for (int i=1;i<=nNodes;++i) {
						if (label[i] < getMin(max,m[idx][i])) {
							label[i] = getMin(max,m[idx][i]);
							prev[i] = idx;
						}
					}
				}
			}
			
			if (quit) {
				break;
			}
		}
		
		System.out.println(total);
		ps.close();
	}
	
	public static int getMin(int a, int b) {
		return a < b ? a : b;
	}
	
}
